﻿<?php

class LuyenUser_OrderController extends Zend_Controller_Action
{
	public function checkoutAction()
	{
		
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		$user = $defaultNamespace->user;
		$cart = $defaultNamespace->carts;
		$defaultNamespace->checkout = true;
		if(($defaultNamespace->step==1) ||((int)($_POST['step_id'])==1)){
			$this->view->step=1;
			if($defaultNamespace->error){
				$this->view->error = $defaultNamespace->error;
				$defaultNamespace->error=null;
			}
			//unset($defaultNamespace->step);
		}
		//var_dump($user->u_email);
		if($user){
			$this->view->user=$user;
			$this->view->carts= $cart;
			$this->view->tongtien = $defaultNamespace->tongtien;
		}
	}
	
	public function billingAction()
	{
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		if($_POST['selectShippingMethod']){
			$defaultNamespace->shipping = (int)($_POST['selectShippingMethod']);
		}else{
			$defaultNamespace->error = "Quý khách phải chọn phương thức giao hàng";
			$defaultNamespace->step=1;
			header('Location: http://shoppingcartcnpm.orgfree.com/index.php/LuyenUser/order/checkout');
		}
		if ((int)($_POST['step_id'])==2){
			$this->view->step = 2;	
		}
	}
	public function orderAction(){
		Zend_Session::start(); 
		$defaultNamespace = new Zend_Session_Namespace('Default');
		if($defaultNamespace->user){
			if($_POST['selectBillingMethod']){
				$defaultNamespace->billing = (int)($_POST['selectBillingMethod']);
			}else{
				$defaultNamespace->error = "Quý khách phải chọn phương thức thanh toán";
				$defaultNamespace->step=2;
				header('Location: http://shoppingcartcnpm.orgfree.com/index.php/LuyenUser/order/shipping');
			}
			if ((int)($_POST['step_id'])==3){
				$this->view->step = 3;
				$orderModel = new Model_Order();
				$orderDetail = new Model_Orderdetail();
				$messagesModel = new Model_Messages();
				
				$u_id = $defaultNamespace->user->u_id;
				$pay_id = $defaultNamespace->billing;
				$ship_id = $defaultNamespace->shipping;
				$os_id=1;
				$ps_id=1; 
				$row=$orderModel->createOrder ($u_id,$pay_id,$ship_id,$os_id,$ps_id);
				//var_dump(time());
				
				$u_id = $row->u_id;
				$m_tieude = "Đơn hàng No".$row->o_id." đã được nhận";
				$m_noidung = $messagesModel->contentMessages();
				$m_dadoc = 0;
				
				$messagesModel->createMessages($u_id,$m_tieude,$m_noidung,$m_dadoc);
				$products = $defaultNamespace->carts;
				foreach ($products as $x){
					$orderDetail->createOrderdetail($x['p_id'],$row->o_id,$x['soluong'],$x['gia']);
					//var_dump($x);   
				}
				$defaultNamespace->carts=null;
				//echo"----------------------------da thanh toan thanh cong-------------------------------------";
			}
		}else{
			header('Location: http://shoppingcartcnpm.orgfree.com');
		}	
	}
	public function ordersearchAction()
	{	
		Zend_Session::start();
		$defaultNamespace = new Zend_Session_Namespace('Default');
		$order = array();
		$i=1;
		$user = $defaultNamespace->user;
		if($user){
			$md = new Model_Order();
			$rowOrder=$md->getOrderByUid($user->u_id);

			$orderStatus = new Model_Orderstatus();
			foreach($rowOrder as $x){
				$rowOrSt = $orderStatus->getOSByID($x->os_id)->current();
				$order[$i]=array('o_id'=>$x->o_id,'o_ngaydat'=>$x->o_ngaydat,'os_ten'=>$rowOrSt->os_ten);
				$i++;
				//echo $rowOrst[0]->os_ten;
				//var_dump($rowOrst[0]->os_ten);
			}
			$this->view->data = $order;
			//var_dump($order);*/
		}else{
			header('Location: http://shoppingcartcnpm.orgfree.com');
		}
	}
	public function orderviewAction()
	{
		Zend_Session::start();
		$defaultNamespace = new Zend_Session_Namespace('Default');
		
		$user = $defaultNamespace->user;
		if($user){
			$orderTable = array();
			$i=1;
			$tongtien=0;
			if($_GET['order_id']){
				$modelOrder = new Model_Order();
				$modelOrderDetail = new Model_Orderdetail();
				$product = new Model_Product();
				
				$order=$modelOrder->getOrderById((int)($_GET['order_id']))->current();
				$pay_id = $order->pay_id;
				
				$modelPayment = new Model_Payment();
				$payment = $modelPayment->getPaymentById($pay_id)->current();
				$pay = $payment->pay_loai;
				$this->view->pay = $pay;  
				
				$modelOrderst = new Model_Orderstatus();
				$os_ten=$modelOrderst->getOSByID($order->os_id)->current()->os_ten;
				$order->os_id = $os_ten;	
				
				$this->view->order = $order;
				$orderDetail = $modelOrderDetail ->getOrderDetailByOrderId($order->o_id);
				foreach($orderDetail as $x){
					//var_dump($x->p_id);
					$orderTable[$i]['sanpham'] = $product->getProductById($x->p_id)->current()->p_ten;
					$orderTable[$i]['gia'] = $x->op_gia;
					$orderTable[$i]['soluong'] = $x->pod_soluong;
					$orderTable[$i]['tong'] = ($x->op_gia)*($x->pod_soluong);
					$tongtien+=($x->op_gia)*($x->pod_soluong);
					$i++;
				}
				$this->view->orderTable = $orderTable;
				$this->view->tongtien = $tongtien;
			}	
		}else{
			header('Location: http://shoppingcartcnpm.orgfree.com');
		}
	}
	public function messagesAction()
	{
		Zend_Session::start();
		$defaultNamespace = new Zend_Session_Namespace('Default');
		
		$user = $defaultNamespace->user;
		if($user){
			$modelMessages = new Model_Messages();
			$messages = $modelMessages->getMessagesByUid($user->u_id);
			$this->view->messages = $messages;
		}else{
			header('Location: http://shoppingcartcnpm.orgfree.com');
		}
	}
	public function messagesdetailAction()
	{
		Zend_Session::start();
		$defaultNamespace = new Zend_Session_Namespace('Default');
		
		$user = $defaultNamespace->user;
		if($user){
			if($_GET['m_id']){
			$modelMessages = new Model_Messages();
			$messages = $modelMessages->setMessagesReaded($_GET['m_id']);
			$this->view->messages = $messages;
		}
		}else{
			header('Location: http://shoppingcartcnpm.orgfree.com');
		}
	}
	public function replymessagesAction()
	{
		Zend_Session::start();
		$defaultNamespace = new Zend_Session_Namespace('Default');
		
		$user = $defaultNamespace->user;
		if($_GET['m_id']){
			$defaultNamespace->m_id = (int)($_GET['m_id']);
		}
		if($user){
				$mdMessages = new Model_Messages();
				$messages = $mdMessages->getMessagesById($defaultNamespace->m_id);
				
				$formMessages = new Form_MessagesForm();
				$formMessages->setMethod('post');
				$formMessages->setAction('replymessages');
					
				$formMessages->getElement('guitoi')->setValue('shoppingcartcnpm@gmail.com');
				$formMessages->getElement('tieude')->setValue($messages->m_tieude);
				$formMessages->getElement('noidung')->setValue($messages->m_noidung);
				//var_dump($formMessages->m_tieude);
				if($_POST){
					$to = $_POST['guitoi'];
					$subject = $_POST['tieude'];
					$message = $_POST['noidung'];
					$from = $user->u_email;
					$headers = "From: $from";
					mail($to,$subject,$message,$headers);
					unset($defaultNamespace->m_id);
					$this->view->messages = "Thư của bạn đã được gửi đi. <br/>Cửa hàng sẽ được chúng tôi xử lý trong vòng 24h";
				}
				$this->view->form = $formMessages;
				
		}else{
			header('Location: http://shoppingcartcnpm.orgfree.com');
		}
		
		
		
	}
	
}
 
